#ifndef SCRIPT_H
#define SCRIPT_H

#include <QString>
#include <QDebug>

#include "scriptFile.h"
#include "command.h"
#include "para.h"

namespace SDHuaYi_Script
{
    extern QString updataScriptFilePath;
    extern QString updataFirmwareFilePath;
	extern QString updataAlgLibFilePath;
	extern QString updataUserAlgLibFilePath;
    extern QString updataNewUid;
    extern QString updataOldUid;

    class script
    {
    public:
        /* 脚本命令列表 */
        QList<command> scriptCmds;
        QList<ScriptLabel> Labels;
        qint32 TestNum;
        HY_RSA_PUBLIC_KEY hY_RSA_PUBLIC_KEY;

        /* 脚本属性 */
        QString Name; // 名称
        qint32 PC;    // 当前命令的行
        QString slog;

        /* 命令暂停、退出标志 */
        bool pauseEn = true;
        bool PauseFlag = false;
        bool ExitFlag = false;

        QString strScript;

        /* cos文件属性 */
        QString cosFilesName;

    private:
        QList<qint32> STACK;
        QList<Para> paras;

    public:
        script();

        /// 根据当前Index设置PC
        void SetCurrentPCByIndex(qint32 curIndex);

        /// 根据字符位置获取当前命令
        command GetCurrentCmdByIndex(qint32 curIndex);

        /// 重置PC
        void ScriptSetPC(qint32 iPC);

        void ScriptBreak();

        /// 从String解析脚本
        QString GetScript(QString ScriptText, QString ScriptPath);

        /// 从文件解析脚本
        void GetScript(QString scriptFileName);

    private:
        /// 入栈函数，用来实现CALL
        void PUSH(qint32 PC);

        /// 出栈函数，用来实现CALL
        qint32 POP();

        /// 获取指令执行地址
        qint32 GetPC(QString label);

        /// 设置进度
        //        void SetProcess(ScriptEventHandle CardTestEvent,qint32 value);

        /// 触发设置返回结果事件
        //        void SetResponse(ScriptEventHandle CardTestEvent, QString result, EnumCmdResultType type);
        /// 触发脚本异常
        /// <param name="value">异常字符串</param>
        //        void SetException(ScriptEventHandle CardTestEvent,QString value);
        /// 执行单条命令
    public:
        QString RunCmd(cardReaderDev reader, QString strCmd, EnumCmdResultType &cmdResultType);

        /// 执行判定语句
    private:
        bool JudgeProcess(command &currentCmd);
        /// 数据处理命令过程
        QString DataOp_Process(command &currentCmd);
        /// 处理Insert
        QString Insert_Process(command &currentCmd, cardReaderDev reader);

        /// 处理APDU
        QString Apdu_Process(command &currentCmd, cardReaderDev reader, EnumCmdResultType &ResultType);
        /// 执行单条指令
    public:
        bool ExcuteCMD(command &currentCmd, cardReaderDev reader = cardReaderDev());
        /// 脚本执行
        /// <param name="cardTestEvent">脚本执行过程中的事件</param>
        //        bool Run(ScriptEventHandle cardTestEvent,  AutoResetEvent runEvent ,cardReaderDev reader,bool ErrStopFlag = true);
        bool Run(cardReaderDev reader, bool ErrStopFlag = true);
    };

}
#endif // SCRIPT_H
